﻿using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.Collections;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace Release
{

    class Release_Check
    {
        private myDT local_dt = new myDT("local");
        private myDT server_dt = new myDT("server");
        private myDT diff_dt = new myDT("diff");
        private myDT copy_dt = new myDT("copy");

        private ArrayList source = new ArrayList();
        private ArrayList target = new ArrayList();
        public string str_cur_status;
        public Boolean form_minimized;
        public string server_pattern;
        public Boolean ignore_server_pattern;

        public delegate void notify_icon_content_change(string s);
        public event notify_icon_content_change change_content;

        public class myDT : DataTable {
            //private string tableName;
            public myDT(string tablename){
                this.TableName = tablename;
                this.Add("release_name");
                this.Add("release_time");
                this.Add("source_folder");
                this.Add("tgt_folder");
            }
            private void Add(string name) {
                this.Columns.Add(name);
            }
        }
        public enum myProj
        {
            nbt,
            entry
        }

        public myDT get_diff_dt() {
            return this.diff_dt;
        }
        public Boolean diff_dt_clear()
        {
            try
            {
                this.diff_dt.Clear();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public Boolean diff_dt_add_row(string rel_name, string rel_time, string source_folder) { 
                try {
                        DataRow s_dir = this.diff_dt.NewRow();
                        s_dir["release_name"] = rel_name;
                        s_dir["release_time"] = rel_time;
                        s_dir["source_folder"] = source_folder;
                        diff_dt.Rows.Add(s_dir);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return false;
                }
            return true;
        }
        public Boolean copy_dt_add_row(string rel_name, string rel_time, string source_folder)
        {
            try
            {
                DataRow s_dir = this.copy_dt.NewRow();
                s_dir["release_name"] = rel_name;
                s_dir["release_time"] = rel_time;
                s_dir["source_folder"] = source_folder;
                copy_dt.Rows.Add(s_dir);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
            return true;
        }
        public Boolean copy_dt_clear()
        {
            try
            {
                this.copy_dt.Clear();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        private string project_name;
        private string local_file;
        private ArrayList src_folders;
        private ArrayList tgt_folders;
        

        public Release_Check(Configure iconfig) {
            this.form_minimized = false;
            this.ignore_server_pattern = !iconfig.active_proj().auto_sync_pattern;
            this.server_pattern = iconfig.active_proj().sync_pattern;
            this.project_name = iconfig.active_proj().proj_name ;
            this.local_file = iconfig.active_proj().records_file;
            this.src_folders = iconfig.active_proj().src_folders;
            this.tgt_folders = iconfig.active_proj().tgt_folders;
        }
        public int copy_diff_folders(string pattern = "", bool ignore_pattern = true) {
            //string src_root_dir;
            //string src_release_dir;
            string src_full_dir;
            string tgt_full_dir;
            int cur_folder_num = 1; ;
            int all_folders_count;
            if (this.diff_dt.Rows.Count > 0)
            {
                try
                {
                    all_folders_count = this.diff_dt.Rows.Count;
                    this.str_cur_status = "Analyzing detected " + all_folders_count.ToString()+ " new folders...";
                    change_content(this.str_cur_status);
                    foreach (string tgt_root_dir in this.tgt_folders)
                    {
                        foreach (DataRow dr in this.diff_dt.Rows)
                        {
                                if (dr["release_name"].ToString().Contains(pattern) || pattern == "")
                                //if ( dr["release_name"].ToString().Equals(pattern)|| pattern == "")
                                {
                                    this.copy_dt_add_row(dr["release_name"].ToString(), dr["release_time"].ToString(), dr["source_folder"].ToString());
                                    this.str_cur_status = "\n" + cur_folder_num.ToString() + "/" + all_folders_count.ToString() + " folder is under copying...";
                                    change_content(this.str_cur_status);
                                    //this.str_cur_status += "\nName: " + dr["release_name"];
                                    src_full_dir = dr["source_folder"] + "\\" + dr["release_name"];
                                    tgt_full_dir = tgt_root_dir + "\\" + dr["release_name"];
                                    DirService.CopyDirectory(src_full_dir, tgt_full_dir, false);
                                    cur_folder_num += 1;
                                }
                                else {
                                    continue;
                                }
                        }
                    }
                    this.str_cur_status = (cur_folder_num-1).ToString() + " folders are copied.";
                    change_content(this.str_cur_status);
                    //this.diff_dt.Rows.Clear();
                    return 2;
                }
                catch (Exception e)
                {
                    MessageBox.Show("problem while copying the folder: [" + e.Message + "]");
                    return 0;
                }
            }
            else {
                    return 1;
            } 
        }
        
        public Boolean save_local()
        {
            if (true)
            {
                try
                {
                    StreamWriter sw = new StreamWriter(this.local_file,true);
                    //int test = 0;
                    foreach (DataRow s in this.copy_dt.Rows)
                    {
                        sw.WriteLine(s["release_name"].ToString() + "\t\t" + s["release_time"].ToString() + "\t\t" + s["source_folder"].ToString());
                        //test += 1;
                    }
                    sw.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "ERROR!");
                    return false;
                }
                return true;
            }
        }
        public Boolean comp_server_local()
        {
            string[] s1 = new string [200];
            string[] s2  = new string [200];
            string[] s11  = new string [200] ;
            string[] s21 = new string[200];
            string[] s111 = new string[200];
            string[] s211 = new string[200];

            long i, j;

            i = 0;
            j = 0;

            if (this.get_from_server() )
            {
                if (this.get_from_local())
                {
                    foreach (DataRow dr in this.server_dt.Rows)
                    {
                        s1[i] = dr["release_name"].ToString();
                        s11[i] = dr["release_time"].ToString();
                        s111[i] = dr["source_folder"].ToString();
                        i += 1;
                    }
                    foreach (DataRow dr1 in this.local_dt.Rows)
                    {
                        s2[j] = dr1["release_name"].ToString();
                        s21[j] = dr1["release_time"].ToString();
                        s211[j] = dr1["source_folder"].ToString();
                        j += 1;
                    }
                    if (this.comp_array(s1, s2, s11, s21,s111, s211))
                    {
                        return true;
                    }
                    else
                    {
                        //this means server and local has the same folder structure.
                        //no need to notice.
                        //MessageBox.Show("No new file detected.", "Info");
                        return false;
                        //this.update_lv();   
                    }
                }
                else {
                    this.diff_dt = this.server_dt;
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        private void change_display_status_info(string s){
//              
            if (this.form_minimized)
            {
                change_content(s);
            }
            else { 
                //main_form.fale_flag
               
            }
        }

        private Boolean get_from_server()
        {
            this.copy_dt_clear();
            foreach (string src_dir in this.src_folders) {
                DirectoryInfo server = new DirectoryInfo(src_dir );
                //if (this.server_dt.Rows.Count != 0)
                //{
                //    return true;
                //}
                try {
                    DirectoryInfo[] dirs = server.GetDirectories();
                    foreach (DirectoryInfo d in dirs)
                    {
                        DataRow s_dir = server_dt.NewRow();
                        s_dir["release_name"] = d.Name;
                        s_dir["release_time"] = d.LastAccessTime;
                        s_dir["source_folder"] = src_dir;
                        server_dt.Rows.Add(s_dir);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return false;
                }

            }
            return true;
        }
        private Boolean get_from_local() {
            string local_text;
            string[] tmp;

            if (!File.Exists(this.local_file)){
                MessageBox.Show("local file not Found! A new file will be created!","Warning!");
                try
                {
                    File.Create(this.local_file).Close();
                }
                catch (Exception e){
                    MessageBox.Show("Error while creating new file " + e.Message, "ERROR!");
                    return false;
                }
                return false;
            }

            try
            {
                StreamReader sr = new StreamReader(this.local_file);
                local_text = sr.ReadLine();
                while (local_text != null)
                {
                    DataRow s_dir = local_dt.NewRow();
                    tmp = local_text.Split('\t');
                    s_dir["release_name"] = tmp[0];
                    s_dir["release_time"] = tmp[2];
                    s_dir["source_folder"] = tmp[4];
                    local_dt.Rows.Add(s_dir);
                    local_text = sr.ReadLine();
                }
                sr.Close();
            }
            catch (Exception e) {
                MessageBox.Show(e.Message, "Error!");
                local_dt.Clear();
                return false;
            }

            return true;
        }
        private Boolean comp_array(string[] s1,string[] s2,string[] s11,string[] s21, string[] s111, string[] s211){
            long i = s1.Length;
            long j = s2.Length;
            long a;
            long b;
            Boolean f;
            Boolean f_a;
            string tmp;
            string tmp1;
            a= 0;
            b = 0;
            f_a = false;

            for(a = 0; a < i; a++){
                f = false;
                tmp = s1[a];
                tmp1 = s111[a];
                if (tmp == ""){
                    continue;
                }
                for(b = 0; b < j;b++){
                    if (string.Compare(tmp,s2[b],true) == 0 && string.Compare(tmp1,s211[b],true) == 0) {
                    //this means we find it. then we ignore this one.
                        f = true;
                        break;
                    }
                    if (s2[b] == null ){
                        break;
                    }
                }
                if (!f){
                    DataRow tmp_dr = this.diff_dt.NewRow();
                    tmp_dr["release_name"] = s1[a];
                    tmp_dr["release_time"] = s11[a];
                    tmp_dr["source_folder"] = s111[a];
                    diff_dt.Rows.Add(tmp_dr);
                    f_a = true;
                }
           }
           if(f_a){
                return true;
           }else{
                return false;
           }
        }
    }
    
}
